﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Web;

public partial class sysadmin_CourseManager_PaperSelect :PowerCreator.LMS.Web.Base.BasePage
{
    //Resource opResource = new Resource();
    ManagerPaper opPaper = new ManagerPaper();
    Course opCourse = new Course();

    #region SQL语句部分

    private string _SQL_CourseResource = "select FKCoursersPaperID from TCourseResource where FKCourseresCourseID={0}  and CoursersClass = 2";

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SearchType"] = "0";//0简单搜索1高级搜索
            ViewState["OrderBy"] = ""; //排序
            ViewState["OrderType"] = " desc ";//排序方式

            ViewState["CourseID"] = 0;
            BinderResourceType();
            string courseID = Request.QueryString["CourseID"].ToString();
            ViewState["CourseID"] = courseID;
            InitPage();
          
           

            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
            BinderPaperClass();
        }
    }
    
    protected override void InitPage()
    {
        lbl_PaperSelect_InfoTitle.Text = GetLanageValueByKey("ResourceSelect_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_PaperName.Text = GetLanageValueByKey("ProjectResourceSet_lbl_l6", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_PaperType.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperType", PowerCreator.LMS.Language.LanagetItemType.Page); 

        btn_Search.Text = GetLanageValueByKey("btn_CourseSelect_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_CourseSelect_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Back.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddResourceRes.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkAll.Text = GetLanageValueByKey("RoleUser_rdbtn_YesNoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);

        VOTOCourse vo = opCourse.LoadVOTCourse(int.Parse(ViewState["CourseID"].ToString()));
        lbl_CourseInfoTitle.Text = vo.CourseName;
        lbl_CourseResourceCount.Text = vo.CourseResourceNum.ToString();
        base.InitPage();
    }
    private void BinderPaperClass()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("Resource_Type", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "-1";
        ddl_PaperType.Items.Insert(0, li);
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("paper_Type1", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "1";
        ddl_PaperType.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = GetLanageValueByKey("paper_Type2", PowerCreator.LMS.Language.LanagetItemType.Page);
        li2.Value = "2";
        ddl_PaperType.Items.Add(li2);
    }

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();

        sbSearch.Append("PaperBuildState=1  and PaperID not in (");
        sbSearch.Append(string.Format(_SQL_CourseResource, ViewState["CourseID"]));
        sbSearch.Append(") and ");

        if (ViewState["SearchType"].ToString() == "1")
        {
            if (!string.IsNullOrEmpty(txt_PaperName.Text))
            {
                sbSearch.Append(string.Format("PaperName like '%{0}%' and ", txt_PaperName.Text));
            }
            if (ddl_PaperType.SelectedValue.ToString() != "-1")
            {
                sbSearch.Append(string.Format(" PaperType = {0} and ", ddl_PaperType.SelectedValue.ToString()));
            }
        }
        else  if (ViewState["SearchType"].ToString() == "0")
        {
            sbSearch.Append(string.Format(" PaperName like '%{0}%' ", txt_SearchKey.Text));
        }
        if (sbSearch.ToString().EndsWith(" and "))
            sbSearch.Remove(sbSearch.Length - 4, 4);
        //Response.Write(sbSearch.ToString());
        //Response.End();
        return sbSearch.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        AspNetPager1.RecordCount = opPaper.CountPaper(search);
    }
    private void BinderRPT(string search,int pageIndex)
    {
        string orderlist = ViewState["OrderBy"].ToString();
        if (!string.IsNullOrEmpty(orderlist))
        {
            orderlist += ViewState["OrderType"].ToString();
        }
        DataTable dt = opPaper.QueryPaper(search, orderlist,LMSPageSize, pageIndex);
        rptPaper.DataSource = dt;
        rptPaper.DataBind();
        PreAspNetPager(AspNetPager1);
    }
    protected void rptPaper_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "leditor")
        {
            CheckBox cbox = (CheckBox)e.Item.FindControl("chkSelect");
            cbox.Checked = true;
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy", "<script>showFloat1()</script>");
            //string url = "AddCoursePaper.aspx?CourseID=" + ViewState["CourseID"] + "&PaperID=" + e.CommandArgument.ToString();
            //Script.RedirectURL(url);
        }
        else if (e.CommandName == "orderby")
        {
            ViewState["OrderBy"] = e.CommandArgument.ToString();
            string search = GetSearch();
            BinderRPT(search, AspNetPager1.CurrentPageIndex);

            if (ViewState["OrderType"].ToString() == " desc")
                ViewState["OrderType"] = " asc";
            else
                ViewState["OrderType"] = " desc";
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "1";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "CourseResource.aspx";
        Script.RedirectURL(url);
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选/取消
        for (int i = 0; i < rptPaper.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptPaper.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptPaper.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptPaper.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void btn_AddResourceRes_Click(object sender, EventArgs e)
    {
        //保存多个课件到课程内
        VOTResourceType voResourceType = new VOTResourceType();
        voResourceType.RestpName = ddl_CoursersRestpID.SelectedItem.Text;
        voResourceType.RestpID = int.Parse(ddl_CoursersRestpID.SelectedItem.Value);
        PowerCreator.LMS.Web.CreatedByInfo cinfo = GetCreatedByInfo();
        VOTOCourse voCoures = opCourse.LoadVOTCourse(int.Parse(ViewState["CourseID"].ToString()));
        CourseresourcePaperInfo paperCourseResource = new CourseresourcePaperInfo();

        for (int i = 0; i < rptPaper.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptPaper.Items[i].FindControl("chkSelect");
            Label lblid = (Label)rptPaper.Items[i].FindControl("lblid");
            if (cbox.Checked)
            {
                string searcht = string.Format("FKCourseresCourseID={0} and FKCoursersPaperID={1} ", ViewState["CourseID"], lblid.Text);
                if (opCourse.QueryCourseResourceCount(searcht) == 0)
                {
                    VOPaper voPaper = opPaper.LoadPaper(int.Parse(lblid.Text));
                    paperCourseResource.PaperID = voPaper.PaperID;
                    paperCourseResource.PaperName = voPaper.PaperName;
                    paperCourseResource.CoursersIndex = 0;
                    paperCourseResource.CoursersEndTime = DateTime.Now;
                    paperCourseResource.CoursersStartTime = DateTime.Now;
                    opCourse.AddCourseResource(null, voResourceType, cinfo, voCoures, null, paperCourseResource, CoursersClass.Paper);
                }
            }
        }
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
        InitPage();
    }

    private void BinderResourceType()
    {
        DataTable dt = new DataTable();
        dt = opCourse.QueryResourceType("");
        ddl_CoursersRestpID.DataSource = dt;
        ddl_CoursersRestpID.DataTextField = "RestpName";
        ddl_CoursersRestpID.DataValueField = "RestpID";
        ddl_CoursersRestpID.DataBind();
    }
}
